package com.app.bhca.demo.tjcahttpdemo;

import com.alibaba.fastjson.JSONObject;
import com.app.bhca.demo.tjcahttpdemo.utils.TJCAHttp;
import netscape.javascript.JSObject;
import org.jb2011.lnf.beautyeye.ch3_button.BEButtonUI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Demo extends JPanel {
    private JTextArea certRichTextBox =new JTextArea(5,30);
    private JTextField cnTextBox = new JTextField("");
    private JTextField uniqueIDTextBox = new JTextField("");
    private JTextArea stampLRichTextBox = new JTextArea(5,30);
    private JButton btnLogin = new JButton("认证登录");
    private JButton btnSign = new JButton("签名");

    private TJCAHttp tjcacom;
    public Demo() {
        super(new BorderLayout());

        initGUI();
        initListeners();
    }

    private void initGUI() {
        // init sub coms
        btnLogin.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.red));
        btnLogin.setForeground(Color.white);

        btnSign.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.red));
        btnSign.setForeground(Color.white);


        // init btn pane
        JPanel btnPane = new JPanel();
        btnPane.setLayout(new FlowLayout(FlowLayout.CENTER));
        btnPane.add(btnLogin);
        btnPane.add(certRichTextBox);
        btnPane.add(cnTextBox);
        btnPane.add(uniqueIDTextBox);
        btnPane.add(stampLRichTextBox);
        btnPane.add(btnSign);


        // init main ui
        this.add(btnPane, BorderLayout.CENTER);
        this.setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100));
    }

    private void initListeners() {
        //初始化控件
        tjcacom = TJCAHttp.GetInstance();


		btnLogin.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e)
			{
                String res =  tjcacom.BHCA_Login(1);

				System.out.println("认证登录");
                System.out.println(res);
                JSONObject jsonObject = JSONObject.parseObject(res);

                String result = jsonObject.getString("result");

                if(!result.equals("0")){
                    String msg = "认证登录失败，错误码：" + result + "，错误信息：" + jsonObject.getString("content");
                    String detail = jsonObject.getString("detail");
                    if(!detail.isEmpty()){
                        msg += detail;
                    }
                    //提示错误信息
                    JOptionPane.showMessageDialog(Demo.this,msg);
                    return;
                }

                JOptionPane.showMessageDialog(Demo.this,"认证登录成功！");

                //解析返回结果

                //填充表单


            }
		});

		btnSign.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String res = tjcacom.BHCA_SignData(1,"123");

                JSONObject jsonObject = JSONObject.parseObject(res);

                String result = jsonObject.getString("result");

                if(!result.equals("0")){
                    String msg = "签名失败，错误码：" + result + "，错误信息：" + jsonObject.getString("content");
                    String detail = jsonObject.getString("detail");
                    if(!detail.isEmpty()){
                        msg += detail;
                    }
                    //提示错误信息
                    JOptionPane.showMessageDialog(Demo.this,msg);
                    return;
                }

                JOptionPane.showMessageDialog(Demo.this,"签名成功！");
            }
        });
    }
}
